package com.leff.cards.ui.util;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.leff.cards.R;
import com.leff.cards.data.BaseGroup;
import com.leff.cards.data.Group;
import com.leff.cards.data.User;

public class GroupListAdapter extends BaseAdapter {

	private Context mContext;
	private ArrayList<? extends BaseGroup> mData;
	
	public GroupListAdapter(Context C) {
		this(C, null);
	}
	public GroupListAdapter(Context C, ArrayList<? extends BaseGroup> data) {
		mContext = C;
		setData(data);
	}
	
	public void setData(ArrayList<? extends BaseGroup> data) {
		mData = data;
		if(mData == null) {
			mData = new ArrayList<BaseGroup>();
		}
		this.notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Object getItem(int pos) {
		return mData.get(pos);
	}

	@Override
	public long getItemId(int pos) {
		return mData.get(pos).id;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		View V = convertView;
		if(V == null) {
			V = LayoutInflater.from(mContext).inflate(R.layout.group_list_item, null);
		}
		
		TextView name = (TextView)V.findViewById(R.id.group_name);
		TextView members = (TextView)V.findViewById(R.id.group_members);
		TextView alert = (TextView)V.findViewById(R.id.alert_text);
		
		BaseGroup BG = mData.get(position);
		ArrayList<User> memberList = BG.members;
		
		name.setText(BG.name);

		int count = memberList.size();
		
		if(BG instanceof Group) {
			Group G = (Group)BG;
			
			int res = count != 1 ? R.string.member_count : R.string.member_count_one;
			String text = String.format(mContext.getResources().getString(res), count, G.voterName);
			members.setText(text);
			
			if(G.members.size() == 1) {
				alert.setTextColor(Color.GREEN);
				alert.setText(R.string.waiting_for_players);
			}
			else if(G.isVoter) {
				if(G.readyForVote) {
					alert.setTextColor(Color.RED);
					alert.setText(R.string.ready_for_vote);
				}
				else {
					alert.setTextColor(Color.GREEN);
					alert.setText(R.string.waiting_for_vote);
				}
			}
			else {
				if(G.hasSubmitted) {
					alert.setTextColor(Color.GREEN);
					alert.setText(R.string.cards_submitted);
				}
				else {
					alert.setTextColor(Color.RED);
					alert.setText(R.string.awaiting_submission);
				}
			}
		}
		else {
			alert.setVisibility(View.GONE);
			
			StringBuilder sb = new StringBuilder();
			
			int res = count != 1 ? R.string.member_list : R.string.member_list_one;
			String cntStr = String.format(mContext.getResources().getString(res), count);
			sb.append(cntStr);
			
			for(User U : memberList) {
				sb.append(U.displayName).append(", ");
			}
			if(count > 0) {
				sb.deleteCharAt(sb.length()-1);
				sb.deleteCharAt(sb.length()-1);
			}
			
			members.setText(sb.toString());
		}
		
		return V;
	}

}
